/*
 * @Author: your name
 * @Date: 2020-09-14 10:53:05
 * @LastEditTime: 2021-04-21 17:12:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \inventory-apie:\hjimi\人脸\html\face-recognition\src\router\index.js
 */
import Vue from 'vue'
import Router from 'vue-router'
// import store from '@/store'
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress'
// import { stringify } from 'qs'
Vue.use(Router)
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}
const constantRoutes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login/index'),
    meta: {
      title: '用户登录',
      hidden: true,
      keepAlive: true
    }
  },
  {
    path: '/home',
    component: () => import('@/views/layout/index'),
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index'),
        meta: {
          title: '首页',
          hidden: true,
          keepAlive: true
        },
      },

// head下拉导航
      {
        path: '/userControl',
        name: 'userControl',
        component: () => import('@/views/userControl/index'),
        meta: {
          title: '用户管理',
          hidden: true,
          keepAlive: true,
          permission: [0, 1] //0为超管，1为普通（隐藏此字段）
        }
      },
      {
        path: '/deviceControl',
        name: 'deviceControl',
        component: () => import('@/views/deviceControl/index'),
        meta: {
          title: '设备管理',
          hidden: true,
          keepAlive: true
        }
      },
      {
        path: '/personalCenter',
        name: 'personalCenter',
        component: () => import('@/views/personalCenter/index'),
        meta: {
          title: '个人中心',
          hidden: true,
          keepAlive: true
        }
      }
    ]
  },
  { path: "*", component: () => import('@/views/404.vue')}

]
const asyncRoutes = [
  {
    path: '/home',
    component: () => import('@/views/layout/index'),
    meta: {
      title: '首页',
    },
    redirect: '/',
    children: [
      // 主页卡片导航
      {
        path: '/dataDisplay',
        name: 'dataDisplay',
        component: () => import('@/views/dataDisplay/index'),
        meta: {
          title: '数据展示',
          hidden: true,
          keepAlive: true
        }
      },
      {
        path: '/witnessThan',
        name: 'witnessThan',
        component: () => import('@/views/witnessThan/index'),
        meta: {
          title: '人证比对',
          hidden: true,
          keepAlive: true
        }
      },
      {
        path: '/cbir',
        name: 'cbir',
        component: () => import('@/views/cbir/index'),
        meta: {
          title: '图像检索',
          hidden: true,
          keepAlive: true
        }
      },
      {
        path: '/propertiesShow',
        name: 'propertiesShow',
        component: () => import('@/views/propertiesShow/propertiesShow'),
        meta: {
          title: '属性展示',
          hidden: true,
          keepAlive: true
        }
      },
      // {
      //   path: '/liveApp',
      //   name: 'liveApp',
      //   component: () => import('@/views/liveApp/index'),
      //   meta: {
      //     title: '场景应用',
      //     hidden: true,
      //     keepAlive: true
      //   }
      // },
      {
        path: '/apiText',
        name: 'apiText',
        component: () => import('@/views/apiText'),
        meta: {
          title: '接口文档',
          hidden: true,
          keepAlive: true
        }
      },
      {
        path: '/faceManagement',
        name: 'faceManagement',
        component: () => import('@/views/faceManagement'),
        meta: {
          title: '脸库管理',
          keepAlive: false
        },
        children: [
          {
            path: '/faceLibraryList',
            name: 'faceLibraryList',
            component: () => import('@/views/faceManagement/faceLibraryList.vue'),
            meta: {
              title: '人脸查询',
              hidden: true,
              keepAlive: false
            }
          }
        ]
      },
      
    ]
  }
]
export const asyncRoutes_list = asyncRoutes[0].children
// 合并路由
asyncRoutes[0].children.map(x => {
  constantRoutes[1].children.push(x)
})
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  document.title = `${to.meta.title}_人脸辨识云`
  const isLogin = getToken()
  
if (isLogin && isLogin !== 'token') { //路由跳转判断是否存在用户信息
    to.path === '/' ? next('/home') : String
    next()
  } else {
    if (to.path === "/") {
      next()
    } else {
      Message({
        message: "登录状态已过期，请重新登录",
        type: "warning",
        duration: 5 * 1000
      });
      next('/')
    }
  }
})

router.afterEach(() => {  
  NProgress.done()
})
export default router 
